package com.xiaozhen.cmdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.cmdb.dao.CmdbExampleValueDao;
import com.xiaozhen.cmdb.service.CmdbExampleValueService;
import com.xiaozhen.common.core.domain.cmdb.CmdbExampleValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/9 16:23
 * @description:
 */
@Service
public class CmdbExampleValueServiceImpl implements CmdbExampleValueService {

    @Autowired
    private CmdbExampleValueDao cmdbExampleValueDao;

    @Override
    public Integer insertCmdbExampleValueList(List<CmdbExampleValue> list, Long currentUserId) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        Long exampleId = list.get(0).getExampleId();
        QueryWrapper<CmdbExampleValue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("example_id", exampleId);
        cmdbExampleValueDao.delete(queryWrapper);
        Integer count = 0;
        for (CmdbExampleValue exampleValue : list) {
            exampleValue.setCreateBy(currentUserId);
            exampleValue.setCreateTime(LocalDateTime.now());
            count += cmdbExampleValueDao.insert(exampleValue);
        }
        return count;
    }
}
